package csc.fresher.view;

import java.io.IOException;
import java.io.Serializable;
import java.util.List;

import javax.faces.context.FacesContext;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import csc.fresher.entity.SystemUser;
import csc.fresher.service.SystemUserService;

@Component
@Scope("session")
public class SystemNavigatePage implements Serializable {
	private static final long serialVersionUID = 1L;

	@Autowired
	private SystemUserService systemUserService;

	@Autowired
	private SystemUserManageBean systemUserManageBean;

	@Autowired
	SystemLoginManageBean systemLoginManageBean;

	@Autowired
	SystemUserManagementManageBean systemUserManagementManageBean;
	
	@Autowired
	SystemUserApprove systemUserApprove;
	
	public String checkLogin() throws IOException {
		if (systemLoginManageBean.getSystemUser() == null) {
		} else {
			systemLoginManageBean.setSystemUser(null);
		}
		return "Logon";
	}
	 
	public String navModifyUser() {
		SystemUser systemUser;
		boolean flag = false;
		if (systemLoginManageBean.getSystemUser() == null) {
			return "Logon.jsf";
		} else {
			systemUser = systemLoginManageBean.getSystemUser();
			List<Long> listrole = systemUserService
					.getRoleByUserName(systemUser.getUserName());
			for (int i = 0; i < listrole.size(); i++) {
				if (1 == listrole.get(i) || 2 == listrole.get(i)) {
					flag = true;
				}
			}
			if (flag == false) {
				return "Logon";

			}
		}
		return "Logon";
	}
	
	public void navChangePassword() {
		SystemUser systemUser;
		boolean flag = false;
		if (systemLoginManageBean.getSystemUser() == null) {
			try {
				FacesContext.getCurrentInstance().getExternalContext()
						.redirect("/csc_final/templates3/Logon.jsf");
			} catch (IOException e) {
				e.printStackTrace();
			}
		} else {
			systemUser = systemLoginManageBean.getSystemUser();
			List<Long> listrole = systemUserService
					.getRoleByUserName(systemUser.getUserName());
			for (int i = 0; i < listrole.size(); i++) {
				if (1 == listrole.get(i) || 2 == listrole.get(i) || 3 == listrole.get(i) || 4 == listrole.get(i)) {
					flag = true;
				}
			}
			if (flag == false) {
				try {
					FacesContext.getCurrentInstance().getExternalContext()
							.redirect("/csc_final/templates3/Logon.jsf");
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
	
	
	public String navUserManagement() {
		systemUserManagementManageBean.init();
		SystemUser systemUser;
		boolean flag = false;
		if (systemLoginManageBean.getSystemUser() == null) {
			return "Logon";
		} else {
			checkIsAdmin();
			systemUser = systemLoginManageBean.getSystemUser();
			List<Long> listrole = systemUserService
					.getRoleByUserName(systemUser.getUserName());
			for (int i = 0; i < listrole.size(); i++) {
				if (1 == listrole.get(i) || 2 == listrole.get(i)) {
					flag = true;
					return "UserManagement";
				}
			}
			if (flag == false) {
				return "Logon";
			
			}
		}
		return "Logon";
	}
	
	public void adminModify() {
		SystemUser systemUser;
		boolean flag = false;
		if (systemLoginManageBean.getSystemUser() == null) {
			try {
				FacesContext.getCurrentInstance().getExternalContext()
						.redirect("/csc_final/templates3/Logon.jsf");
			} catch (IOException e) {
				e.printStackTrace();
			}
		} else {
			checkIsAdmin();
			systemUser = systemLoginManageBean.getSystemUser();
			List<Long> listrole = systemUserService
					.getRoleByUserName(systemUser.getUserName());
			for (int i = 0; i < listrole.size(); i++) {
				if (1 == listrole.get(i) || 2 == listrole.get(i) || 3 == listrole.get(i) || 4 == listrole.get(i)) {
					flag = true;
				}
			}
			if (flag == false) {
				try {
					FacesContext.getCurrentInstance().getExternalContext()
							.redirect("/csc_final/templates3/Logon.jsf");
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
	
	public void userSupportRole() {
		SystemUser systemUser;
		boolean flag = false;
		if (systemLoginManageBean.getSystemUser() == null) {
			try {
				FacesContext.getCurrentInstance().getExternalContext()
						.redirect("/csc_final/templates3/Logon.jsf");
			} catch (IOException e) {
				e.printStackTrace();
			}
		} else {
			checkIsAdmin();
			systemUser = systemLoginManageBean.getSystemUser();
			List<Long> listrole = systemUserService
					.getRoleByUserName(systemUser.getUserName());
			for (int i = 0; i < listrole.size(); i++) {
				if (1 == listrole.get(i) || 2 == listrole.get(i)) {
					flag = true;
				}
			}
			if (flag == false) {
				try {
					FacesContext.getCurrentInstance().getExternalContext()
							.redirect("/csc_final/templates3/Logon.jsf");
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}

	public void userAdminRole() {
		
		SystemUser systemUser;
		boolean flag = false;
		if (systemLoginManageBean.getSystemUser() == null) {
			try {
				FacesContext.getCurrentInstance().getExternalContext()
						.redirect("/csc_final/templates3/Logon.jsf");
			} catch (IOException e) {
				e.printStackTrace();
			}
		} else {
			checkIsAdmin();
			systemUser = systemLoginManageBean.getSystemUser();
			List<Long> listrole = systemUserService
					.getRoleByUserName(systemUser.getUserName());
			for (int i = 0; i < listrole.size(); i++) {
				if (1 == listrole.get(i)) {
					flag = true;
				}
			}
			if (flag == false) {
				try {
					FacesContext
							.getCurrentInstance()
							.getExternalContext()
							.redirect(
									"/csc_final/templates3/UserManagement.jsf");
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
	
	public void navApproveUser() {
		
		SystemUser systemUser;
		boolean flag = false;
		if (systemLoginManageBean.getSystemUser() == null) {
			try {
				FacesContext.getCurrentInstance().getExternalContext()
						.redirect("/csc_final/templates3/Logon.jsf");
			} catch (IOException e) {
				e.printStackTrace();
			}
		} else {
			checkIsAdmin();
			systemUserApprove.checkNull();
			systemUserApprove.init();
			systemUser = systemLoginManageBean.getSystemUser();
			List<Long> listrole = systemUserService
					.getRoleByUserName(systemUser.getUserName());
			for (int i = 0; i < listrole.size(); i++) {
				if (1 == listrole.get(i)) {
					flag = true;
				}
			}
			if (flag == false) {
				try {
					FacesContext
							.getCurrentInstance()
							.getExternalContext()
							.redirect(
									"/csc_final/templates3/UserManagement.jsf");
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
	public boolean checkIsAdmin() {
		SystemUser systemUser;
		boolean flag = false;

		systemUser = systemLoginManageBean.getSystemUser();
		List<Long> listrole = systemUserService.getRoleByUserName(systemUser
				.getUserName());
		for (int i = 0; i < listrole.size(); i++) {
			if (1 == listrole.get(i)) {
				flag = true;
			}
		}
		return flag;
	}

}
